package com.fyp.tests;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/*
 *  ============================================================================
 *  This file is part of 4YProject.
 *
 *  4YProject is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  4YProject is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with 4YProject; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *  ============================================================================
 */

/**
 * ============================================================================
 * The following class TestDBSelect.java provides a simple test case that will
 * see if a connection can be established with a database.
 * 
 * @version 1.0
 * @author Sean Crowley
 * @date 17 Jan 2008
 * ============================================================================
 */

public class TestDBSelect {

	private static String driverClass = "org.apache.derby.jdbc.EmbeddedDriver";
	private static String connectionURL = "jdbc:derby:/DerbyDB/AssetDB";
	Connection con = null;

	/**
	 * This method will create a connection to the database
	 */
	public TestDBSelect() {
		try {
			System.out.print("  Loading JDBC Driver  -> " + driverClass + "\n");
			Class.forName(driverClass).newInstance();
			System.out.print("  Connecting to        -> " + connectionURL + "\n");
			this.con = DriverManager.getConnection(connectionURL);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * This method will perform the query search
	 */
	public void performQuery() {
		Statement stmt = null;
		ResultSet rset = null;
		String queryString = "SELECT * " + "FROM USERS ";
		try {
			System.out.print("  Creating Statement...\n");
			stmt = con.createStatement();
			System.out.print("  Opening ResultsSet...\n");
			rset = stmt.executeQuery(queryString);

			int counter = 0;

			while (rset.next()) {
				System.out.println();
				System.out.println("  Row [" + ++counter + "]");
				System.out.println("  ---------------------");
				System.out.println("      First Name       -> " + rset.getString(1));
				System.out.println("      Surname          -> " + rset.getString(2));
				System.out.println("      Pay Grade        -> " + rset.getFloat(3));
			}

			System.out.println();
			System.out.print("  Closing ResultSet...\n");
			rset.close();

			System.out.print("  Closing Statement...\n");
			stmt.close();

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * This method will close the connection
	 */
	public void closeConnection() {
		try {
			System.out.print("  Closing Connection...\n");
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args)
			throws java.lang.InterruptedException {

		TestDBSelect qe = new TestDBSelect();
		qe.performQuery();
		qe.closeConnection();
	}
}
